#include "slannewprojectwidget.h"

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QFileDialog>


#include <QHBoxLayout>
#include <QVBoxLayout>


SlanNewProjectWidget::SlanNewProjectWidget(QWidget *parent)
    : QDialog(parent)
{
    pNameLabel = new QLabel(tr("项目名称:"), parent);
    pNameEdit = new QLineEdit(parent);

    QHBoxLayout *nameLayout = new QHBoxLayout;
    nameLayout->addWidget(pNameLabel);
    nameLayout->addWidget(pNameEdit);

    pPathLabel = new QLabel(tr("项目路径:"), this);
    pPathEdit =  new QLineEdit(this);
    pathButton = new QPushButton(tr("..."), this);
    pathButton->setMaximumWidth(30);

    QHBoxLayout *pathLayout = new QHBoxLayout;
    pathLayout->addWidget(pPathLabel);
    pathLayout->addWidget(pPathEdit);
    pathLayout->addWidget(pathButton);

    pOkButton = new QPushButton(tr("确定"), this);
    pOkButton->setEnabled(false);
    pCancelButton = new QPushButton(tr("取消"), this);
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(pOkButton);
    buttonLayout->addWidget(pCancelButton);



    QVBoxLayout *widgetLayout = new QVBoxLayout(this);
    widgetLayout->addLayout(nameLayout);
    widgetLayout->addLayout(pathLayout);
    widgetLayout->addLayout(buttonLayout);

    this->setLayout(widgetLayout);

    connect(pCancelButton, &QPushButton::clicked, this, &SlanNewProjectWidget::close);
    connect(pathButton, &QPushButton::clicked, this, &SlanNewProjectWidget::slotSelectDir);
    connect(pOkButton, &QPushButton::clicked, this, &SlanNewProjectWidget::slotOk);
    connect(pNameEdit, &QLineEdit::textChanged, this, &SlanNewProjectWidget::slotSetOkButton);
}

QString SlanNewProjectWidget::getName()
{
    return projectName;
}

QString SlanNewProjectWidget::getDirPath()
{
    return projectDirPath;
}


//选择目录，并填充到pPathEdit
void SlanNewProjectWidget::slotSelectDir()
{
    QString dirName = QFileDialog::getExistingDirectory(this, tr("打开"));
    if ( !dirName.isEmpty() )
    {
        pPathEdit->setText(dirName);
    }
}

//确定功能实现
void SlanNewProjectWidget::slotOk()
{
    projectName = pNameEdit->text();
    projectDirPath = pPathEdit->text();
    this->accept();
}

//根据项目名输入框的状态判定确定键是否可用
void SlanNewProjectWidget::slotSetOkButton(const QString &text)
{
    if ( text.isEmpty() )
        pOkButton->setEnabled(false);
    else
        pOkButton->setEnabled(true);
}
